package com.test.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.test.annotation.RequirePermission;
import com.test.domain.Company;
import com.test.domain.User;
import com.test.service.CompanyService;
import com.test.service.RoleService;
import com.test.service.UserService;
import com.test.vo.Result;

@Controller
@RequestMapping("user")
public class UserController {

	@Autowired
	private UserService userService;
	
	@Autowired
	private CompanyService companyService;
	
	@RequirePermission("user:listUI")
	@RequestMapping("listUI")
	public String listUI(HttpServletRequest request) {
		return "user/listUI";
	}
	
	@RequirePermission("user:listUI")
	@RequestMapping("list")
	@ResponseBody
	public Result list(int offset, int limit,String search) {
		
		PageInfo<User> pageInfo = this.userService.getListByPage(offset / limit + 1, limit, search);
		return Result.succeed(pageInfo);
	}
	
	
//===================================保存/修改/删除方法=======================================	

	//跳转修改密码
	@RequestMapping("set")
	public String set() {
		return "user/set";
	}

	@RequestMapping("setPassword")
	@ResponseBody
	public Result setPassword(String newPassword, HttpSession session) {
		User user= (User) session.getAttribute("loginUser");
		if(this.userService.updatePassword((user.getId()).intValue(),newPassword)) {
			user.setPassword(newPassword);
			session.setAttribute("loginUser", user);
			return Result.succeed("../manageUI");
		} else
			return Result.fail(404, "请求出现错误!");
	}



	@RequestMapping("saveUI")
	public ModelAndView saveUI(Integer id,HttpServletRequest request) {
		ModelAndView ma = new ModelAndView();
		if (id != null) {
			User user = this.userService.getById(id);
			if (user != null) {
				List<Company> companyList=companyService.getList();
				request.setAttribute("companyList",companyList);
				request.setAttribute("user", user);
			}
		}else
		{
			List<Company> companyList=companyService.getList();
			ma.addObject(companyList);
			ma.setViewName("user/saveUI");
		}
		return ma;
		//return "user/saveUI";
	}
	
	
	@RequirePermission("user:add;user:update")
	@RequestMapping("save")
	public String add(User user) {
		if (user.getId() != null) {
			this.userService.update(user);
		} else {
			user.setCreateTime(new Date());
			user.setUpdateTime(user.getCreateTime());
			user.setPassword(DigestUtils.md5Hex("123456"));
			this.userService.save(user);
		}
		return "redirect:/user/listUI";
	}


	@RequirePermission("user:delete")
	@RequestMapping("delete/{ids}")
	@ResponseBody
	public Result delete(@PathVariable("ids") String ids) {
		
		String[] idsStr = ids.split(",");
		if (idsStr.length == 1) {
			this.userService.deleteById(Integer.parseInt(idsStr[0]));
		} else {
			this.userService.deleteBatchByIds(idsStr);
		}
		return Result.succeed();
	}
	
	@RequirePermission("user:setRole")
	@RequestMapping("setRole")
	@ResponseBody
	public Result setRole(int userId,String roleIds) {
		
		this.userService.saveUserRole(userId,roleIds);
		
		return Result.succeed();
	}
	@RequestMapping(value = "checkName")
	@ResponseBody
	public String checkName(String userName)
	{
		User user=userService.findUserByUserName(userName);
		if(user!=null){
			return "fail";
		}
		return "ok";
	}
}
